package top.hcode.hoj.manager.coll;

import cn.hutool.core.util.BooleanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import top.hcode.hoj.dao.collect.CollTrainingRecordDao;
import top.hcode.hoj.pojo.dto.coll.SaveCollTrainingRecordDTO;
import top.hcode.hoj.pojo.entity.coll.CollTrainingRecord;

import java.util.ArrayList;
import java.util.List;

/**
 * @author orangej
 * @since 2025/1/21
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class ItechCollTrainingRecordManager {
    private final CollTrainingRecordDao trainingRecordDao;
    public List<CollTrainingRecord> saveTrainingRecord(List<SaveCollTrainingRecordDTO> records) {
        List<CollTrainingRecord> result = new ArrayList<>();

        for (SaveCollTrainingRecordDTO record : records) {
            if (BooleanUtil.isTrue(record.getUpToNow())) {
                record.setEndDate(null);
            }

            // 保存
            trainingRecordDao.saveOrUpdate(record);

            // 返回
            result.add(record);
        }

        return result;
    }


    public List<CollTrainingRecord> getTrainingRecordList(String uid, String collId) {
        return trainingRecordDao.getTrainingRecordList(uid, collId);
    }

    public CollTrainingRecord deleteTrainingRecord(String uid, Long id) {
        String strId = String.valueOf(id);
        CollTrainingRecord record = trainingRecordDao.getById(strId);
        if (record != null && record.getUserId().equals(uid)) {
            trainingRecordDao.removeById(strId);
        }
        return record;
    }

    public Integer countTrainingRecord(String uid, String collId) {
        return trainingRecordDao.countTrainingRecord(uid, collId);
    }
}
